package com.powernode.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.Order;
import com.powernode.model.Result;
import com.powernode.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;
/**
 * @author RenBoQing
 * @date 2024年08月27日 22:01
 * @Description 订单业务模块控制器
 */
@Api(tags = "订单业务接口管理")
@RequestMapping("order/order")
@RestController
public class SysOrederController {

    @Autowired
    private OrderService orderService;

    /*
     * 多条件分页查询订单
     * @author RenBoQing
     * @date 2024/8/27 22:09
     * @param current
     * @param size
     * @param orderNumber
     * @param status
     * @param startTime
     * @param endTime
     * Page<com.powernode.domain.Order>>
     */
    @ApiOperation("多条件分页查询订单")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('order:order:page')")
    public Result<Page<Order>> loadOrderPage(@RequestParam Long current, @RequestParam Long size, @RequestParam(required = false) String orderNumber, @RequestParam(required = false) Integer status, @RequestParam(required = false) Date startTime, @RequestParam(required = false) Date endTime) {
        // 创建订单分页对象
        Page<Order> page = new Page<>(current, size);
        // 多条件分页查询订单
        page = orderService.queryOrderPage(page, orderNumber, status, startTime, endTime);
        return Result.success(page);
    }

    /*
     * 根据订单编号查询订单的详情
     * @author RenBoQing
     * @date 2024/8/29 22:10
     * @param orderNumber
     * @return com.powernode.model.Result<com.powernode.domain.Order>
     */
    @ApiOperation("根据订单编号查询订单的详情")
    @GetMapping("orderInfo/{orderNumber}")
    @PreAuthorize("hasAnyAuthority('order:order:info')")
    public Result<Order> loadOrderDetail(@PathVariable Long orderNumber) {
        // 查询对应的数据
        Order order = orderService.queryOrderDetailByOrderNumber(orderNumber);
        // 返回对应的订单数据
        return Result.success(order);
    }
    /*
     * 销售记录导出为excel
     * @author RenBoQing
     * @date 2024/8/31 22:34
     * @return com.powernode.model.Result<java.lang.String>
     */
    @ApiOperation("销售记录导出为excel")
    @GetMapping("/soldExcel")
    @PreAuthorize("hasAnyAuthority('order:order:soldExcel')")
    public Result<String> exportSlodOrder() {
        // 查询所有的销售记录
        List<Order> orderList = orderService.list(
                new LambdaQueryWrapper<Order>()
                        .orderByDesc(Order::getCreateTime)
        );
        // 定义对应的文件位置
        String fileName = "D:\\course\\19-PowerMall\\" + System.currentTimeMillis() + ".xlsx";
        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
        // 如果这里想使用03 则 传入excelType参数即可
        EasyExcel.write(fileName, Order.class).sheet("模板111").doWrite(orderList);
        return Result.success("orderList");
    }
}
